package com.yc.bank.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class MyBatisHelper {
    static SqlSessionFactory factory;


    static {
        String resource = "mybatis.xml";
        //读入配置
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
            //构建会话工厂   绘画 ~= 连接
            factory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            throw new RuntimeException("Mybatis初始化失败!",e);
        }
    }


    /**
     * 创建sql会话并返回，会话必须手动关闭
     */
    public static SqlSession getSession(){
        return factory.openSession();
    }
} 
